import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: 老姚
 * Date:  2020/5/18 23:06
 */

public class test {
    @Test
    public void test01() {
        int[] nums = {2, 7, 11, 15};
        int target = 13;
        test.twoSum(nums, target);
    }

    /**
     * 2数之和      * @param arrs
     * @param target
     */
    public static void twoSum(int[] arrs, Integer target) {

        Map<Integer, Integer> map = new HashMap<>();
        int[] res = new int[2];
        for (int i = 0; i < arrs.length; i++) {
            if (map.containsKey(target - arrs[i])) {
                res[0] = i;
                res[1] = map.get(target - arrs[i]);
                break;
            }
            map.put(arrs[i], i);
        }
        for (int re : res) {
            System.out.println(re);
        }
    }
}
